home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cool / ge_cool.lha / GE_COOL2.1 / src / Vector / Base_Vector.C next >
C/C++ Source or Header  |  1992-04-23  |  11KB  |  305 lines

  1. //
  2. // Copyright (C) 1991 Texas Instruments Incorporated.
  3. //
  4. // Permission is granted to any individual or institution to use, copy, modify,
  5. // and distribute this software, provided that this complete copyright and
  6. // permission notice is maintained, intact, in all copies and supporting
  7. // documentation.
  8. //
  9. // Texas Instruments Incorporated provides this software "as is" without
  10. // express or implied warranty.
  11. //
  12. // Created:  MBN 08/31/89 -- Initial design and implementation
  13. // Updated:  VDN 02/21/92 -- new lite version
  14. //
  15. // The CoolVector class is no longer derived from the Generic class  and is used to
  16. // implement non-type specific functionality for the parameterized CoolVector<Type>
  17. // class. In this manner, code common to all instances of  the CoolVector class can
  18. // be shared to reduce code  replication. The CoolVector<Type>  class is dynamic in
  19. // the sense that an object can grow dynamically if necessary.  The growth size
  20. // is determined by the  value  of a  static  allocation size variable  for the
  21. // class.  However, fixed length vectors  are also  supported by  setting  this
  22. // variable  to INVALID.   The CoolVector class  implements the notion of a current
  23. // position. This is useful  for iterating through  the  elements of a  vector.
  24. // The current position is maintained in an integer and  is set or reset by all
  25. // methods  affecting elements in the  CoolVector class. Methods to reset,  move to
  26. // the next and previous, find,  and get the value at  the current position are
  27. // provided.
  28. //
  29.  
  30. #ifndef BASE_VECTORH                        // If no CoolVector class
  31. #include <cool/Base_Vector.h>            // Include the class header
  32. #endif
  33.  
  34. #ifdef __cplusplus
  35. float CoolVector::growth_ratio = 0.0;        // Initialize growth ratio
  36. #endif
  37.  
  38.  
  39. // CoolVector () -- Empty constructor for the CoolVector class.
  40. // Input:       None
  41. // Output:      None
  42.  
  43. CoolVector::CoolVector () {
  44.   this->size = 0;                // No storage allocated
  45.   this->number_elements = 0;            // No elements in CoolVector
  46.   this->curpos = INVALID;            // Invalidate current position
  47.   alloc_size = MEM_BLK_SZ;            // Set the default size 
  48. #ifndef __cplusplus
  49.   if (this->growth_ratio == 0.0)        // If not initialized
  50.     this->growth_ratio = 0.0;            // Initialize growth ratio
  51. #endif
  52. }
  53.  
  54. // CoolVector (long) -- Constructor that specifies number of elements.
  55. // Input:           Number of elements 
  56. // Output:          None
  57.  
  58. CoolVector::CoolVector (long n) {
  59.   this->size = n;                // Element capacity
  60.   this->number_elements = 0;            // No elements in Vector
  61.   this->curpos = INVALID;            // Invalidate current position
  62.   alloc_size = MEM_BLK_SZ;            // Set memory block size
  63. #ifndef __cplusplus
  64.   if (this->growth_ratio == 0.0)        // If not initialized
  65.     this->growth_ratio = 0.0;            // Initialize growth ratio
  66. #endif
  67. }
  68.  
  69. // CoolVector (CoolVector&) -- Constructor for reference to another CoolVector 
  70. // Input:              CoolVector reference
  71. // Output:             None
  72.  
  73. CoolVector::CoolVector (const CoolVector& v) {
  74. #ifndef __cplusplus
  75.   if (this->growth_ratio == 0.0)        // If not initialized
  76.     this->growth_ratio = 0.0;            // Initialize growth ratio
  77. #endif
  78.   alloc_size = v.alloc_size;            // Set the default size 
  79.   if (alloc_size == INVALID)            // If invalid allocation size
  80.     alloc_size = MEM_BLK_SZ;            // Set default size
  81.   this->size = v.size;                // Copy element capacity
  82.   this->number_elements = v.number_elements;    // Same number of elements
  83.   this->curpos = INVALID;            // Invalidate current position
  84. }
  85.  
  86. // ~CoolVector () -- Destructor for CoolVector class that frees up storage.
  87. // Input:        None
  88. // Output:       None
  89.  
  90. CoolVector::~CoolVector() {
  91. }
  92.  
  93. // CoolVector& operator= () -- Overload the assignment operator
  94. // Input:                  Reference to CoolVector object
  95. // Output:                 Reference to copied Vector object
  96.  
  97. CoolVector& CoolVector::operator= (const CoolVector& v) {
  98.   this->number_elements = v.number_elements;    // Copy number of elements
  99.   this->growth_ratio = v.growth_ratio;        // Copy the growth ratio
  100.   this->curpos = INVALID;            // Invalidate current position
  101.   return *this;                    // Return CoolVector reference
  102. }
  103.  
  104. // clear() -- Removes all elements from this CoolVector
  105. //            (included for compatability with List)
  106. // Input:     None.
  107. // Output:    None.
  108.  
  109. void CoolVector::clear() {
  110.   this->reset();                        // make current position invalid   
  111.   this->number_elements = 0;
  112. }
  113.  
  114. // is_empty() -- Indicates empty CoolVector
  115. //               (included for compatability with List)
  116. // Input:        None.
  117. // Output:       TRUE or FALSE.
  118.  
  119. Boolean CoolVector::is_empty() {
  120.   return this->number_elements == 0;
  121. }
  122.  
  123. // long set_length () -- Set the number of elements in a CoolVector object.
  124. //                       If there is not enough storage allocated, set to
  125. //                       maximum size for storage.
  126. // Input:                Length, type 
  127. // Output:               Integer representing number of elements
  128.  
  129. long CoolVector::set_length (long n, const char* Type) {
  130. #if ERROR_CHECKING
  131.   if (n < 0) {            // If index out of range
  132.     //RAISE (Error, SYM(CoolVector), SYM(Negative_Length),
  133.     printf ("CoolVector<%s>::set_length(): Negative length %d.\n", Type, n);
  134.     abort ();
  135.   }
  136. #endif                        
  137.   if (n <= size)                // If not greater than size
  138.     this->number_elements = n;            // Set new length
  139.   else
  140.     this->number_elements = size;        // Else set to maximum possible
  141.   return this->number_elements;            // Return value
  142. }
  143.  
  144. // void set_growth_ratio (float) -- Set the growth percentage for the CoolVector
  145. //                                  object.
  146. // Input:                           Float ratio, type
  147. // Output:                          None
  148.  
  149. void CoolVector::set_growth_ratio (float ratio, const char* Type) {
  150. #if ERROR_CHECKING
  151.   if (ratio <= 0.0) {                // If non-positive growth
  152.     //RAISE (Error, SYM(CoolVector), SYM(Negative_Ratio),
  153.     printf ("CoolVector<%s>::set_growth_ratio(): Negative growth ratio %f.\n",
  154.         Type, ratio);
  155.     abort ();
  156.   }
  157. #endif
  158.   this->growth_ratio = ratio;            // Adjust ration
  159. }
  160.  
  161.  
  162. // void set_alloc_size (int) -- Set the default allocation size growth rate.
  163. // Input:                       Growth size in number of elements, type
  164. // Output:                      None
  165.  
  166. void CoolVector::set_alloc_size (int n, const char* Type) {
  167. #if ERROR_CHECKING
  168.   if (n < 0) {                    // If index out of range
  169.     //RAISE (Error, SYM(CoolVector), SYM(Negative_Size),
  170.     printf ("CoolVector<%s>::set_alloc_size(): Negative growth size %d.\n",
  171.         Type, n);
  172.     abort ();
  173.   }
  174. #endif
  175.   this->alloc_size = n;                // Set growth size
  176. }
  177.  
  178. //----------------------------no error exception----------------------------
  179.  
  180.  
  181. // bracket_error -- Raise exception for CoolVector<Type>::operator[]()
  182. // Input:           Type string
  183. // Output:          None
  184.  
  185. void CoolVector::bracket_error (const char* Type, const long n) const {
  186.   //RAISE (Error, SYM(CoolVector), SYM(Out_Of_Range),
  187.   printf ("CoolVector<%s>::operator[](): Index %d out of range.\n", Type, n);
  188.   abort ();
  189. }
  190.  
  191. // value_error -- Raise exception for CoolVector<Type>::value()
  192. // Input:         Type string
  193. // Output:        None
  194.  
  195. void CoolVector::value_error (const char* Type) const {
  196.   //RAISE (Error, SYM(CoolVector), SYM(Invalid_Cpos),
  197.   printf ("CoolVector<%s>::value(): Invalid current position.\n", Type);
  198.   abort ();
  199. }
  200.  
  201. // resize_error -- Raise exception for CoolVector<Type>::resize()
  202. // Input:          Type string, size argument
  203. // Output:         None
  204.  
  205. void CoolVector::resize_error (const char* Type, const long n) const {
  206.   //RAISE (Error, SYM(CoolVector), SYM(Negative_Size),
  207.   printf ("CoolVector<%s>::resize(): Negative resize %d.\n", Type, n);
  208.   abort ();
  209. }
  210.  
  211. // static_error -- Raise exception for CoolVector<Type>::resize()
  212. // Input:          Type string, size argument
  213. // Output:         None
  214.  
  215. void CoolVector::static_error (const char* Type) const {
  216.   //RAISE (Error, SYM(CoolVector), SYM(Static_Size),
  217.   printf ("CoolVector<%s>::resize(): Static-size CoolVector.\n", Type);
  218.   abort ();
  219. }
  220.  
  221. // assign_error -- Raise exception for CoolVector<Type>::operator=()
  222. // Input:          Type string
  223. // Output:         None
  224.  
  225. void CoolVector::assign_error (const char* Type) const {
  226.   //RAISE (Error, SYM(CoolVector), SYM(Static_Size),
  227.   printf ("CoolVector<%s>::operator=(): Static-size CoolVector.\n", Type);
  228.   abort ();
  229. }
  230.  
  231. // fill_start_error -- Raise exception for CoolVector<Type>::fill()
  232. // Input:              Type string, start argument
  233. // Output:             None
  234.  
  235. void CoolVector::fill_start_error (const char* Type, const long start) const {
  236.   //RAISE (Error, SYM(CoolVector), SYM(Out_Of_Range),
  237.   printf ("CoolVector<%s>::fill(): Start index %d out of range.\n", Type, start);
  238.   abort ();
  239. }
  240.  
  241.  
  242. // fill_end_error -- Raise exception for CoolVector<Type>::fill()
  243. // Input:            Type string, end argument
  244. // Output:           None
  245.  
  246. void CoolVector::fill_end_error (const char* Type, const long end) const {
  247.   //RAISE (Error, SYM(CoolVector), SYM(Out_Of_Range),
  248.   printf ("CoolVector<%s>::fill(): End index %d out of range.\n", Type, end);
  249.   abort ();
  250. }
  251.  
  252. // copy_start_error -- Raise exception for CoolVector<Type>::copy()
  253. // Input:              Type string, start argument
  254. // Output:             None
  255.  
  256. void CoolVector::copy_start_error (const char* Type, const long start) const {
  257.   //RAISE (Error, SYM(CoolVector), SYM(Out_Of_Range),
  258.   printf ("CoolVector<%s>::copy(): Start index %d out of range.\n", Type, start);
  259.   abort ();
  260. }
  261.  
  262.  
  263. // copy_end_error -- Raise exception for CoolVector<Type>::copy()
  264. // Input:            Type string, end argument
  265. // Output:           None
  266.  
  267. void CoolVector::copy_end_error (const char* Type, const long end) const {
  268.   //RAISE (Error, SYM(CoolVector), SYM(Out_Of_Range),
  269.   printf ("CoolVector<%s>::copy(): End index %d out of range.\n", Type, end);
  270.   abort ();
  271. }
  272.  
  273. // copy_error -- Raise exception for CoolVector<Type>::copy()
  274. // Input:        Type string
  275. // Output:       None
  276.  
  277. void CoolVector::copy_error (const char* Type) {
  278.   //RAISE (Error, SYM(CoolVector), SYM(Static_Size),
  279.   printf ("CoolVector<%s>::copy(): Static-size CoolVector.\n", Type);
  280.   abort ();
  281. }
  282.  
  283.  
  284. // remove_error -- Raise exception for CoolVector<Type>::remove()
  285. // Input:          Type string
  286. // Output:         None
  287.  
  288. void CoolVector::remove_error (const char* Type) const { 
  289.   //RAISE (Error, SYM(CoolVector), SYM(Invalid_Cpos),
  290.   printf ("CoolVector<%s>::remove(): Invalid current position.\n", Type);
  291.   abort ();
  292. }
  293.  
  294. // va_arg_error -- Raise exception for using class objects, or chars in (...)
  295. // Input:          Type string
  296. // Output:         None
  297.  
  298. void CoolVector::va_arg_error (const char* Type, int n) {
  299.   //RAISE (Error, SYM(CoolVector), SYM(Invalid_Va_Arg),
  300.   printf ("CoolVector<%s>::CoolVector<%s>(): Invalid type in ... or wrong alignment with %d bytes.\n",
  301.       Type, Type, n);
  302.   abort ();
  303. }
  304.  
  305.